package com.meshnetics.gb.type.impl.zcl;

import com.meshnetics.gb.type.DataTypeID;
import com.meshnetics.gb.type.DataValue;

/**
 * WRITE DESCRIPTION HERE
 *
 * @author AKovylin
 */
public class TypeGeneralData extends FixedBytesDataType {
    private static final String ALIAS_PREFIX = "general";
    private static final String ALIAS_POSTFIX = "bit";
    private static final String ID_TYPE_CLASS = "General Data";
    private static final String ID_DATA_TYPE_POSTFIX = "-bit data";

    public static final TypeGeneralData GENERAL_8 = new TypeGeneralData(1,
            new DataTypeID((byte) 0x08, ID_TYPE_CLASS, 8 + ID_DATA_TYPE_POSTFIX));
    public static final TypeGeneralData GENERAL_16 = new TypeGeneralData(2,
            new DataTypeID((byte) 0x09, ID_TYPE_CLASS, 16 + ID_DATA_TYPE_POSTFIX));
    public static final TypeGeneralData GENERAL_24 = new TypeGeneralData(3,
            new DataTypeID((byte) 0x0a, ID_TYPE_CLASS, 24 + ID_DATA_TYPE_POSTFIX));
    public static final TypeGeneralData GENERAL_32 = new TypeGeneralData(4,
            new DataTypeID((byte) 0x0b, ID_TYPE_CLASS, 32 + ID_DATA_TYPE_POSTFIX));
    public static final TypeGeneralData GENERAL_40 = new TypeGeneralData(5,
            new DataTypeID((byte) 0x0c, ID_TYPE_CLASS, 40 + ID_DATA_TYPE_POSTFIX));
    public static final TypeGeneralData GENERAL_48 = new TypeGeneralData(6,
            new DataTypeID((byte) 0x0d, ID_TYPE_CLASS, 48 + ID_DATA_TYPE_POSTFIX));
    public static final TypeGeneralData GENERAL_56 = new TypeGeneralData(7,
            new DataTypeID((byte) 0x0e, ID_TYPE_CLASS, 56 + ID_DATA_TYPE_POSTFIX));
    public static final TypeGeneralData GENERAL_64 = new TypeGeneralData(8,
            new DataTypeID((byte) 0x0f, ID_TYPE_CLASS, 64 + ID_DATA_TYPE_POSTFIX));

    protected TypeGeneralData(int byteSize, DataTypeID dataTypeId) {
        super(new String[]{ALIAS_PREFIX + (byteSize * BITS_IN_BYTE) + ALIAS_POSTFIX}, dataTypeId, byteSize);
    }

    public DataValue fromObject(Object value) {
        if (value instanceof Number) {
            return super.fromObject(value);
        } else {
            if (!(value instanceof byte[])) {
                throw new IllegalArgumentException("value must be bytes array");
            }
            return valueOf((byte[]) value);
        }
    }

    // no byte array cloning needed
    protected FixedBytesDataValue createValue(byte[] bytes) {
        return new GeneralData(this, bytes);
    }
}
